﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using LongQuanWeb.App_Code;
using System.IO;
using Maticsoft.Common;

namespace LongQuanWeb.Admin
{
    public partial class ActiveAddUpdate : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if ((this.Session["username"] == null) || (this.Session["username"].ToString() == string.Empty))
            {
                base.Response.Write("<font color=#ff0000 style='FONT-SIZE: 12px'>对不起,您没足够权限访问此页!!</font><br>");
                base.Response.Write("<a href='Login.aspx' target=_top style='FONT-SIZE: 12px'>重新登陆</a><br>");
                base.Response.End();
            }
            if (!IsPostBack)
            {
                base.Response.Write("<script>parent.window.document.getElementById( 'lbl_title').innerHTML='新闻编辑'; </script>");
                string type = Request.QueryString["type"].ToString().Trim();
                if (type == "1")
                {
                    this.txt_createtime.Text = DateTime.Now.ToString("yyyy-MM-dd");
                }
                if (type == "2")
                {
                    string id = Request.QueryString["id"].ToString().Trim();
                    string sql = "select * from [Activity] where AcitivityNum=" + id + "";
                    DataSet ds = DbHelperSQL.Query(sql);
                    if (ds.Tables[0] != null && ds.Tables[0].Rows.Count != 0)
                    {
                        this.txt_title.Text = ds.Tables[0].Rows[0]["ActivityName"].ToString().Trim();
                        this.txt_source.Text = ds.Tables[0].Rows[0]["Source"].ToString().Trim();
                        this.txt_label.Text = ds.Tables[0].Rows[0]["ActivityDescription"].ToString().Trim();
                        this.txtContent.Value = ds.Tables[0].Rows[0]["ActivityContent"].ToString();
                        this.txt_createtime.Text = DateTime.Parse(ds.Tables[0].Rows[0]["createtime"].ToString().Trim()).ToString("yyyy-MM-dd");
                        this.img1.Src = @"../Upload/Active/" + ds.Tables[0].Rows[0]["PicPath"].ToString().Trim() + "";
                    }
                }
                btn_return.CommandArgument = type;
            }
        }

        protected void btn_save_Click(object sender, EventArgs e)
        {
            string type = btn_return.CommandArgument.ToString();
            try
            {
                if (type == "1")
                {
                    string strSql = "insert into [Activity](ActivityName,ActivityDescription,ActivityContent,PicPath,createtime,Source,SubCategoryType) "
                                  + "values('" + this.txt_title.Text.Trim() + "','" + this.txt_label.Text.Trim() + "','" + this.txtContent.Value.Replace("'", "''").Trim() + "','" + this.imgpath.Value.Trim() + "','" + this.txt_createtime.Text.Trim() + "','" + this.txt_source.Text.Trim() + "','1') ";
                    if (DbHelperSQL.ExecuteSql(strSql) > 0)
                    {
                        base.Response.Write("<script>alert('添加成功');window.location='ActiveAddUpdate.aspx?type=1'</script>");
                    }
                    else
                    {
                        base.Response.Write("<script>alert('添加失败');</script>");
                    }
                }
                else if (type == "2")
                {
                    string id = Request.QueryString["id"].ToString().Trim();
                    //保存旧数据
                    string strSql = "Select PicPath From [Activity] where AcitivityNum=" + id.Trim() + "";
                    DataTable dt = DbHelperSQL.Query(strSql).Tables[0];
                    string filepath = System.Web.HttpContext.Current.Server.MapPath(@"~/Upload/Active/" + dt.Rows[0]["PicPath"].ToString().Trim() + "");
                    //如果 没有修改图片 保存以前图片
                    if (this.imgpath.Value == "")
                        imgpath.Value = dt.Rows[0]["PicPath"].ToString().Trim();
                    string sql = "Update [Activity] set ActivityName='" + this.txt_title.Text.Trim() + "',ActivityDescription='" + this.txt_label.Text.Trim() + "',ActivityContent='" + this.txtContent.Value.Replace("'", "''").Trim() + "',Source='" + this.txt_source.Text.Trim() + "',createtime='" + this.txt_createtime.Text.Trim() + "',PicPath='" + this.imgpath.Value.Trim() + "' where AcitivityNum=" + id + "";
                    if (DbHelperSQL.ExecuteSql(sql) > 0)
                    {
                        //如果图片未修改，则不删除图片
                        if (File.Exists(filepath) && imgpath.Value != dt.Rows[0]["PicPath"].ToString().Trim())//判断文件是不是存在           
                        {
                            File.Delete(filepath);
                        }
                        base.Response.Write("<script>alert('修改成功');window.location='ActiveList.aspx'</script>");
                    }
                    else
                    {
                        base.Response.Write("<script>alert('修改失败');</script>");
                    }
                }
            }
            catch (Exception er)
            {
                base.Response.Write("<script>alert('修改失败');</script>");
                //base.Response.Write("<script language=javascript>alert('" + er.Message + "');</script>");
            }
        }

        protected void btn_return_Click(object sender, EventArgs e)
        {
            string type = btn_return.CommandArgument.ToString();
            Response.Redirect("ActiveList.aspx");
        }

        protected void btn_upload1_Click(object sender, EventArgs e)
        {
            string type = btn_return.CommandArgument.ToString();
            //string strSql = "Select PicPath From [Activity] where ID=" + Num.Trim() + " and ImageFlag=1";
            //DataTable dt = DbHelperSQL.Query(strSql).Tables[0];

            /// 1、没有上传的文件
            /// 2、类型不允许
            /// 3、大小超限
            /// 4、未知错误
            /// 0、上传成功。
            DotNet.Utilities.ImageUpload iu = new DotNet.Utilities.ImageUpload();
            iu.SavePath = @"~/Upload/Active/";
            iu.FormFile = File1;
            iu.IsCreateImg = false;
            iu.Upload();

            if (iu.Error == 1)
            {
                base.Response.Write("<script>alert('没有上传的文件');</script>");
                return;
            }

            if (iu.Error == 2)
            {
                base.Response.Write("<script>alert('上传失败，不支持该类型文件上传');</script>");
                return;
            }

            if (iu.Error == 3)
            {
                base.Response.Write("<script>alert('上传失败，图片过大');</script>");
                return;
            }

            if (iu.Error == 4)
            {
                base.Response.Write("<script>alert('上传失败，未知错误');</script>");
                return;
            }

            if (iu.Error == 0)
            {
                this.imgpath.Value = iu.OutFileName.Trim();
                base.Response.Write("<script>alert('上传成功');</script>");
                return;
            }

        }
    }
}
